package www.etslink.cn.keyboardmaster.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import www.etslink.cn.keyboardmaster.R;

/**
 * Created by Flyhooo on 2017/7/12 17:00
 */

public class ETSPinDialog extends Dialog {


    private Context mContext;

    private String mTitle;
    private ETSPinInputListener mListener;

    public ETSPinDialog(Context context) {
        super(context, R.style.Dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ets_pindialog);
        keys[0] = (TextView) findViewById(R.id.dialog_key0);
        keys[1] = (TextView) findViewById(R.id.dialog_key1);
        keys[2] = (TextView) findViewById(R.id.dialog_key2);
        keys[3] = (TextView) findViewById(R.id.dialog_key3);
        keys[4] = (TextView) findViewById(R.id.dialog_key4);
        keys[5] = (TextView) findViewById(R.id.dialog_key5);
        keys[6] = (TextView) findViewById(R.id.dialog_key6);
        keys[7] = (TextView) findViewById(R.id.dialog_key7);
        keys[8] = (TextView) findViewById(R.id.dialog_key8);
        keys[9] = (TextView) findViewById(R.id.dialog_key9);

        results[0] = (TextView) findViewById(R.id.dialog_result0);
        results[1] = (TextView) findViewById(R.id.dialog_result1);
        results[2] = (TextView) findViewById(R.id.dialog_result2);
        results[3] = (TextView) findViewById(R.id.dialog_result3);
        results[4] = (TextView) findViewById(R.id.dialog_result4);
        results[5] = (TextView) findViewById(R.id.dialog_result5);

        deleteAll = (TextView) findViewById(R.id.dialog_key_deleteAll);
        delete = (TextView) findViewById(R.id.dialog_key_delete);

        closeIV = (ImageView) findViewById(R.id.dialog_close);
        titleTV = (TextView) findViewById(R.id.dialog_title);

        closeIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (mListener != null) {
                    mListener.onCancle();
                }
            }
        });

        titleTV.setText(TextUtils.isEmpty(mTitle) ? "请输入密码" : mTitle);

        deleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (count != 0) {
                    for (TextView textView : results) {
                        textView.setText("");
                    }
                    count = 0;
                    resultSB.delete(0, resultSB.length());
                }
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 1; i < 7; i++) {
                    if (i == count) {
                        count--;
                        results[count].setText("");
                        resultSB.deleteCharAt(count);
                    }
                }
            }
        });

        for (final TextView textView : keys) {
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (count < 6) {
                        results[count].setText("*");
                        resultSB.append(textView.getText());
                        count++;
                        if (count == 6 && mListener != null) {
                            mListener.onResult(resultSB.toString());
                            textView.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    dismiss();
                                }
                            }, 300);
                        }
                    }
                }
            });
        }

    }

    int count = 0;
    StringBuilder resultSB = new StringBuilder();

    private TextView[] keys = new TextView[10];
    private TextView[] results = new TextView[6];

    private TextView deleteAll, delete;

    private ImageView closeIV;
    private TextView titleTV;

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(0, 0, 0, 0);

        getWindow().setAttributes(layoutParams);

    }

    public static class Builder {

        ETSPinDialog dialog;

        public Builder(Context context) {
            dialog = new ETSPinDialog(context);
            dialog.mContext = context;
        }

        /**
         * 设置对话框标题
         *
         * @param title
         */
        public Builder setTitle(String title) {
            dialog.mTitle = title;
            return this;
        }

        /**
         * 设置回调
         *
         * @param listener
         * @return
         */
        public Builder setListener(ETSPinInputListener listener) {
            dialog.mListener = listener;
            return this;
        }

        /**
         * 通过Builder类设置完属性后构造对话框的方法
         */
        public ETSPinDialog create() {
            return dialog;
        }
    }


}
